package edu.guet.server.controller;

import edu.guet.server.model.TecherCourse;
import edu.guet.server.model.User;
import edu.guet.server.model.dto.TeamTeacherDTO;
import edu.guet.server.service.CourseService;
import edu.guet.server.service.TechSemService;
import edu.guet.server.service.UserService;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TechSemService techSemService;
    @Autowired
    UserService userService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 为课程添加教学团队成员
     * @param teamTeacherDTO
     * @return
     */
    @Transactional
    @PostMapping("/addteam")
    public ResultAPI addTeamTeacher(@RequestBody TeamTeacherDTO teamTeacherDTO){
        TecherCourse techerCourse =techSemService.findcourseIdsBytechId(teamTeacherDTO);
        if(techerCourse!=null){
            return ResultAPI.success(null,"该教师已在团队中!");
        }
        //查找老师
        TeamTeacherDTO t=techSemService.findUserByUsernameorEmail(teamTeacherDTO);
        //添加老师
        techSemService.addTeamTeacher(t);
        //course添加老师
//        courseService.addTechCourse(t.getUser_id(),t.getCourse_id());
        return ResultAPI.success(t,"请求成功！");
    }

    /**
     * 展示教师团队
     * @param course_id
     * @return
     */
    @GetMapping("/showteam")
    public ResultAPI showTeamTeacher(@RequestParam Integer course_id){
        log.info("展示教师团队course_id：{}", course_id);
        //搜索老师ids
        List<Integer> techIds=techSemService.findtechIdsBycouId(course_id);
        log.info("techIds:{}", techIds);
        //搜索教师username和头像
        List<TeamTeacherDTO> teacherDTOs=null;
        if(techIds.size()>0){
            teacherDTOs=techSemService.findUsersByIds(techIds);}
        log.info("teacherDTOs:{}", teacherDTOs);
        return ResultAPI.success(teacherDTOs,"请求成功！");
    }
    @PostMapping("/showTeacher")
    public ResultAPI showTeacher(@RequestParam Integer tech_id){
        User user=userService.getUser(tech_id);
        return ResultAPI.success(user);
    }

    @PutMapping("/updateTeacher")
    public ResultAPI updateTeacher(User user, MultipartFile file){
        String Url=null;
//        if(file!=null){
//            try {
//                Url = aliOSSUtils.upload(file);
//            } catch (Exception e) {
//                log.info("文件存OSS失败！");
//                return ResultAPI.error("文件上传失误，请求失败！");
//            }
//            Url="https://mynoteapp-web.oss-cn-beijing.aliyuncs.com/f06cf4fc-0ff2-4e3a-b5ec-246fb6514034.png";
//            user.setImg_url(Url);
//        }
        userService.updateUser(user);
        return ResultAPI.success(user);
    }
}
